package com.qtenv.datasync.api.ningbo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

public class NingBoApi {
    private final static String host = "http://116.62.124.32:25000/znbpt/v1.0.0";
    private final static String appId = "zc";
    private final static String secret = "zc@122";
    private final static String companyCode = "00126";

    private static String token = login().getStr("accessToken");

    private static JSONObject post(String url, Map<String, Object> paramMap, boolean header) {
        HttpRequest post = HttpUtil.createPost(host + url);

        if (header) {
            post.header("Authorization", "Bearer " + token);
        }

        String result = post.contentType("application/json").body(JSONUtil.toJsonStr(paramMap)).execute().body();
        return JSONUtil.parseObj(result);
    }

    private static JSONObject post(String url, Map<String, Object> paramMap) {
        return post(url, paramMap, true);
    }

    private static JSONObject postNoHeader(String url, Map<String, Object> paramMap) {
        return post(url, paramMap, false);
    }

    private static JSONObject postForm(String url, String body, boolean header) {
        HttpRequest post = HttpUtil.createPost(host + url);

        if (header) {
            post.header("Authorization", "Bearer " + token);
        }

        String result = post.contentType("application/x-www-form-urlencoded").body(body).execute().body();
        return JSONUtil.parseObj(result);
    }

    private static JSONObject postForm(String url, String body) {
        return postForm(url, body, true);
    }

    private static JSONObject get(String url, String body) {
        HttpRequest get = HttpUtil.createGet(host + url).header("Authorization", "Bearer " + token);
        String result = get.body(body).execute().body();
        return JSONUtil.parseObj(result);
    }

    public static JSONObject login() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", appId);
        paramMap.put("secret", secret);

        return postNoHeader("/login", paramMap);
    }

    public static JSONObject devices(String startTime, String endTime) {
        return get("/devices", "companyCode=" + companyCode + "&startTime=" + startTime + "&endTime=" + endTime);
    }

    public static JSONObject readings(String startTime, String endTime) {
        return get("/readings", "companyCode=" + companyCode + "&startTime=" + startTime + "&endTime=" + endTime);
    }

    public static JSONObject readings(String meterCode, String startTime, String endTime) {
        return get(String.format("/readings/%s/%s", companyCode, meterCode), "startTime=" + startTime + "&endTime=" + endTime);
    }

    public static JSONObject readingsOfMeters(String meterCodes, String startTime, String endTime) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", appId);
        paramMap.put("secret", secret);
        return postForm("/readingsOfMeters", "companyCode=" + companyCode + "&meterCodes=" + meterCodes + "&startTime=" + startTime + "&endTime=" + endTime);
    }

    public static JSONObject readingsHistory(String startTime, String endTime) {
        return get("/nonresident/readings/history", "companyCode=" + companyCode + "&startTime=" + startTime + "&endTime=" + endTime);
    }

    public static void main(String[] args) {
//        System.out.println(readingsHistory("2023-11-15","2023-11-15"));
//        System.out.println(readings("191100136", "2024-11-28", "2024-11-28"));
        System.out.println(readings("6222000655", "2024-11-28", "2024-11-28"));
//        System.out.println(readingsOfMeters("191100136,191100138", "2024-11-28", "2024-11-28"));
    }
}
